// pages/msg_notice_add_details/msg_notice_add_details.js
let app = getApp();
var my_lid = ''

Page({

    /**
     * 页面的初始数据
     */
    data: {
        array1: [{
                'LNAME': '单股'
            },
            {
                'LNAME': '双股'
            },
            {
                'LNAME': '三股'
            },
        ],
        zhuangtai:false,
        LYLNAME: "",
        LYLCODE: '',
        LLBATCH: '',
        LLWEIGHT: "",
        LTSREMARK: "",
        LLCOLORCODE: "",
        LLCOLORNAME: "",
        LNUMBER: ''
    },
    bindinputip(e) {
        console.log('内容：', e.detail.value)
        if (e.currentTarget.dataset.id == '1') {
            this.data.LYLNAME = e.detail.value
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.LYLCODE = e.detail.value
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.LLBATCH = e.detail.value
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.LLWEIGHT = e.detail.value
        } else if (e.currentTarget.dataset.id == '5') {
            this.data.LTSREMARK = e.detail.value
        } else if (e.currentTarget.dataset.id == '6') {
            this.data.LLCOLORCODE = e.detail.value
        } else if (e.currentTarget.dataset.id == '7') {
            this.data.LLCOLORNAME = e.detail.value
        }
    },
    bindinputip2(e) {
        let that = this
        if (e.currentTarget.dataset.id == '1') {
            this.data.table[0].LYLNAME = e.detail.value
            this.setData({
                table: this.data.table
            })
            console.log(this.data.table)
        } else if (e.currentTarget.dataset.id == '2') {
            this.data.table[0].LYLCODE = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '3') {
            this.data.table[0].LLBATCH = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '4') {
            this.data.table[0].LLWEIGHT = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '5') {
            this.data.table[0].LTSREMARK = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '6') {
            this.data.table[0].LLCOLORCODE = e.detail.value
            this.setData({
                table: this.data.table
            })
        } else if (e.currentTarget.dataset.id == '7') {
            this.data.table[0].LLCOLORNAME = e.detail.value
            this.setData({
                table: this.data.table
            })
        }
    },
    bindPickerChange1(e) {
        this.setData({
            index1: e.detail.value,
        })
        if (e.currentTarget.dataset.id == 1) {
            for (var i = 0; i < this.data.array1.length; i++) {
                if (i == e.detail.value) {
                    this.setData({
                        LNUMBER: this.data.array1[i].LNAME
                    })
                }
            }
        } else {
            for (var i = 0; i < this.data.array1.length; i++) {
                if (i == e.detail.value) {
                    this.data.table[0].LNUMBER = this.data.array1[i].LNAME
                    this.setData({
                        table: this.data.table
                    })
                }
            }
        }

    },
    update1() {
        let that = this
        if (my_lid == undefined) {
            if (that.data.LYLNAME == '') {
                wx.showModal({
                    title: '提示',
                    content: '请输入原料名称',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        }
                    }
                })
            } else if (that.data.LYLCODE == '') {
                wx.showModal({
                    title: '提示',
                    content: '请输入原料编号',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            console.log('用户点击确定')
                        }
                    }
                })
            } else {
                app.globalData.my_table = {
                    "LYLNAME": that.data.LYLNAME, //原料名称
                    'LYLCODE': that.data.LYLCODE, //原料编号
                    'LLCOLORCODE': that.data.LLCOLORCODE, // 色号
                    'LLCOLORNAME': that.data.LLCOLORNAME, // 颜色
                    // 'LLBATCH': that.data.LLBATCH, // 批号
                    // 'LNUMBER': that.data.LNUMBER, // 股数
                    'LLWEIGHT': that.data.LLWEIGHT, // 数量
                    'LTSREMARK': that.data.LTSREMARK, // 备注
                }
                wx.navigateBack({
                    delta: 1
                })
            }
        } else {
            let table = {
                'table': [{
                    'LID': my_lid,
                    "LYLNAME": that.data.LYLNAME, //原料名称
                    'LYLCODE': that.data.LYLCODE, //原料编号
                    'LLCOLORCODE': that.data.LLCOLORCODE, // 色号
                    'LLCOLORNAME': that.data.LLCOLORNAME, // 颜色
                    // 'LLBATCH': that.data.LLBATCH, // 批号
                    // 'LNUMBER': that.data.LNUMBER, // 股数
                    'LLWEIGHT': that.data.LLWEIGHT, // 数量
                    'LTSREMARK': that.data.LTSREMARK, // 备注
                }]
            }
            wx.request({
                url: 'https://' + app.globalData.url + '/Contract/addContractDts',
                data: {
                    "model": JSON.stringify(table),
                },
                method: 'POST',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                    if (res.data.success == true) {
                        wx.navigateBack({
                            delta: 1
                        })
                        wx.showToast({
                            title: '成功',
                            icon: 'success',
                            duration: 2000
                        })
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '添加失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {

                                }
                            }
                        })
                    }

                },
                fail: function (res) {
                    that.setData({
                        loadModal: false
                    })
                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                },
            })
        }


    },
    update2(e) {
        let that = this
        let table = {
            'table': that.data.table
        }
        wx.request({
            url: 'https://' + app.globalData.url + '/Contract/updateContractDts',
            data: {
                "model": JSON.stringify(table),
            },
            method: 'POST',
            dataType: 'json',
            responseType: 'text',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function (res) {
                if (res.data.success == true) {
                    wx.navigateBack({
                        delta: 1
                    })
                    wx.showToast({
                        title: '成功',
                        icon: 'success',
                        duration: 2000
                    })
                } else {
                    wx.showModal({
                        title: '提示',
                        content: '添加失败',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {

                            }
                        }
                    })
                }

            },
            fail: function (res) {
                that.setData({
                    loadModal: false
                })
                wx.showModal({
                    title: '提示',
                    content: '访问服务器出错',
                    showCancel: false,
                    confirmText: '确定',
                    success(res) {
                        if (res.confirm) {

                        }
                    }
                })
            },
        })
    },
    add_color() {
        wx.navigateTo({
            url: '../msg_color/msg_color'
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this
        app.globalData.my_table = ''
        app.globalData.colorcode = ''
        my_lid = options.lid
        this.setData({
            option: options.list
        })
        if(app.globalData.lstaterun == 12) {
            that.setData({
                zhuangtai:true,
            })
        }
        if (options.list == 2) {
            wx.request({
                url: 'https://' + app.globalData.url + '/Contract/findContractDts',
                data: {
                    LID: app.globalData.notice_lid,
                    LSEQ: app.globalData.notice_lseq,
                    LMAKEOPID: app.globalData.notice_lmakeopid
                },
                method: 'GET',
                dataType: 'json',
                responseType: 'text',
                header: {
                    'content-type': 'application/json;charset=utf-8'
                },
                success: function (res) {
                    if (res.data.success == true) {
                        that.setData({
                            table: res.data.obj
                        })
                        for (var i = 0; i < that.data.array1.length; i++) {
                            if (that.data.table[0].LNUMBER == that.data.array1[i].LNAME) {
                                that.setData({
                                    index1: i
                                })
                            }
                        }
                    } else {
                        wx.showModal({
                            title: '提示',
                            content: '获取数据失败',
                            showCancel: false,
                            confirmText: '确定',
                            success(res) {
                                if (res.confirm) {}
                            }
                        })
                    }
                },
                fail: function (res) {
                    that.setData({
                        loadModal: false
                    })
                    wx.showModal({
                        title: '提示',
                        content: '访问服务器出错',
                        showCancel: false,
                        confirmText: '确定',
                        success(res) {
                            if (res.confirm) {}
                        }
                    })
                }
            })
        }
        console.log(options.lid)
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        if (app.globalData.colorcode != '') {
            this.setData({
                LCOLORCODE: app.globalData.colorcode,
                LCOLORNAME: app.globalData.colorname
            })
        }
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})